package cn.edu.nju.software.cdiy.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.nju.software.cdiy.bean.Scheme;
import cn.edu.nju.software.cdiy.bean.Uorder;
import cn.edu.nju.software.cdiy.bean.User;
import cn.edu.nju.software.cdiy.dao.SchemeDao;
import cn.edu.nju.software.cdiy.dao.UorderDao;
import cn.edu.nju.software.cdiy.dao.ValueDao;
import cn.edu.nju.software.cdiy.service.UorderService;
import cn.edu.nju.software.cdiy.util.BusinessConstant;

public class UorderServiceImpl implements UorderService {

	@Autowired
	private UorderDao uorderDao;
	@Autowired
	private SchemeDao schemeDao;
	@Autowired
	private ValueDao valueDao;

	public void setUorderDao(UorderDao uorderDao) {
		this.uorderDao = uorderDao;
	}

	public void setSchemeDao(SchemeDao schemeDao) {
		this.schemeDao = schemeDao;
	}

	public void setValueDao(ValueDao valueDao) {
		this.valueDao = valueDao;
	}

	@Override
	public Uorder saveUorder(Uorder uorder) {
		Scheme scheme = schemeDao.getSchemeById(uorder.getScheme().getId());
		uorder.setScheme(scheme);
		uorder.setPrice(scheme.getPrice());
		uorder.setTime(new Date(System.currentTimeMillis()));
		uorder.setState(valueDao.getValue(BusinessConstant.UORDER_STATE1));
		return this.uorderDao.saveUorder(uorder);
	}

	@Override
	public List<Uorder> getUserUorders(User user) {
		return this.uorderDao.getUserUorders(user);
	}

	@Override
	public Uorder loadByUorderId(Long uorderId) {
		return this.uorderDao.getUorderById(uorderId);
	}

	@Override
	public boolean setState(Uorder uorder, String uorderType) {
		return this.uorderDao.saveUorderState(uorder,valueDao.getValue(uorderType));
	}

}
